
import os
import configparser
import shutil
def load_config():
    cfg = configparser.ConfigParser()
    cfg.read("config.ini",encoding='utf-8')
    return {
        "backup2dir":cfg.get('BackUpTo','dir')
    }
def load_files():
    '''
    初级版本先从指定的文件中获取文件列表
    files4backup.txt 文件 一行一个路径 暂时只支持文件
    @return list of path to backup
    '''
    fs = []
    with open("files4backup.txt",'r',encoding='utf-8') as f:
        for l in f.readlines():
            l2p = os.path.abspath(l.strip())
            if len(l.strip())>0 and os.path.exists(l2p):
                fs.append(l2p)
    return fs
def check_config():
    assert os.path.exists("files4backup.txt"),"files4backup.txt不存在"
    assert os.path.exists("config.ini"),"files4backup.txt不存在"

def check_config_CFG(c):
    pass 
def go(_C,_Fs):
    '''
    _C config
    _Fs files
    '''
    top = _C['backup2dir']
    try:
        if not os.path.exists(top):
            os.makedirs(top)
        for f in _Fs:
            shutil.copy2(f,os.path.join(top,os.path.basename(f)))
    except Exception as e:
        raise "出现异常"+str(e)

if __name__ == "__main__":
    check_config()
    CFG = load_config()
    check_config_CFG(CFG)
    files = load_files()
    go(CFG,files)
